#include <uci.h>
#include <stdlib.h>
#include <string.h>
#include "modbus_config.h"

// 单例模式下的全局静态变量，保存配置文件解析结果
static ConfigData g_ConfigData;

int parse_config_file(){

    struct uci_section *uci_sect;
    const char *tmp;
    struct uci_context *uci_ctx = uci_alloc_context();
    if (uci_ctx == NULL)
    {
        printf("[%s][%d]alloc uci context fail!\n", __func__, __LINE__);
        return -1;
    }

    struct uci_package *uci_pack = NULL;
    if (uci_load(uci_ctx, modbus_config_file, &uci_pack) != UCI_OK)
    {
        printf("[%s][%d]open uci config fail!\n", __func__, __LINE__);
        uci_free_context(uci_ctx);
        return -1;
    }


    /*master1 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_MASTER1);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.master1.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.master1.enable is %s\n", __func__, __LINE__, g_ConfigData.master1.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, PORT);
    if (tmp != NULL)
    {
        g_ConfigData.master1.port = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.master1.port is %d\n", __func__, __LINE__, g_ConfigData.master1.port);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, PERIOD);
    if (tmp != NULL)
    {
        g_ConfigData.master1.period = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.master1.period is %d\n", __func__, __LINE__, g_ConfigData.master1.period);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, REQUEST_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.master1.request_slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.master1.request_slave_id is %d\n", __func__, __LINE__, g_ConfigData.master1.request_slave_id);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, MASTER_IP);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.master1.request_server_ip, tmp);
    }
    printf("[%s][%d] g_ConfigData.master1.request_server_ip is %s\n", __func__, __LINE__, g_ConfigData.master1.request_server_ip);


    /*master2 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_MASTER2);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci master2 section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.master2.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.master2.enable is %s\n", __func__, __LINE__, g_ConfigData.master2.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, PORT);
    if (tmp != NULL)
    {
        g_ConfigData.master2.port = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.master2.port is %d\n", __func__, __LINE__, g_ConfigData.master2.port);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, PERIOD);
    if (tmp != NULL)
    {
        g_ConfigData.master2.period = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.master2.period is %d\n", __func__, __LINE__, g_ConfigData.master2.period);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, REQUEST_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.master2.request_slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.master2.request_slave_id is %d\n", __func__, __LINE__, g_ConfigData.master2.request_slave_id);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, MASTER_IP);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.master2.request_server_ip, tmp);
    }
    printf("[%s][%d] g_ConfigData.master2.request_server_ip is %s\n", __func__, __LINE__, g_ConfigData.master2.request_server_ip);

    /*slava1 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_SLAVE1);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci slave1 section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.slave1.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.slave1.enable is %s\n", __func__, __LINE__, g_ConfigData.slave1.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, PORT);
    if (tmp != NULL)
    {
        g_ConfigData.slave1.port = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.slave1.port is %d\n", __func__, __LINE__, g_ConfigData.slave1.port);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, SLAVE_IP);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.slave1.server_ip, tmp);
    }
    printf("[%s][%d] g_ConfigData.slave1.server_ip is %s\n", __func__, __LINE__, g_ConfigData.slave1.server_ip);

    /*slava2 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_SLAVE2);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci slave2 section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.slave2.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.slave2.enable is %s\n", __func__, __LINE__, g_ConfigData.slave2.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, PORT);
    if (tmp != NULL)
    {
        g_ConfigData.slave2.port = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.slave2.port is %d\n", __func__, __LINE__, g_ConfigData.slave2.port);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, SLAVE_IP);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.slave2.server_ip, tmp);
    }
    printf("[%s][%d] g_ConfigData.slave2.server_ip is %s\n", __func__, __LINE__, g_ConfigData.slave2.server_ip);

    /*uart2 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_UART2);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci uart2 section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.uart2.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.uart2.enable is %s\n", __func__, __LINE__, g_ConfigData.uart2.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_BAUD);
    if (tmp != NULL)
    {
        g_ConfigData.uart2.baud = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart2.baud is %d\n", __func__, __LINE__, g_ConfigData.uart2.baud);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_DATA_BIT);
    if (tmp != NULL)
    {
        g_ConfigData.uart2.data_bit = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart2.data_bit is %d\n", __func__, __LINE__, g_ConfigData.uart2.data_bit);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_PARITY);
    if (tmp != NULL)
    {
        g_ConfigData.uart2.parity = *tmp;
    }
    printf("[%s][%d] g_ConfigData.uart2.parity is %c\n", __func__, __LINE__, g_ConfigData.uart2.parity);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_STOP_BIT);
    if (tmp != NULL)
    {
        g_ConfigData.uart2.stop_bit = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart2.stop_bit is %d\n", __func__, __LINE__, g_ConfigData.uart2.stop_bit);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.uart2.slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart2.slave_id is %d\n", __func__, __LINE__, g_ConfigData.uart2.slave_id);   

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, REQUEST_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.uart2.request_slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart2.request_slave_id is %d\n", __func__, __LINE__, g_ConfigData.uart2.request_slave_id);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_MODE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.uart2.mode, tmp);
    }
    printf("[%s][%d] g_ConfigData.uart2.mode is %s\n", __func__, __LINE__, g_ConfigData.uart2.mode);


    /*uart3 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_UART3);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci uart3 section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.uart3.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.uart3.enable is %s\n", __func__, __LINE__, g_ConfigData.uart3.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_BAUD);
    if (tmp != NULL)
    {
        g_ConfigData.uart3.baud = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart3.baud is %d\n", __func__, __LINE__, g_ConfigData.uart3.baud);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_DATA_BIT);
    if (tmp != NULL)
    {
        g_ConfigData.uart3.data_bit = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart3.data_bit is %d\n", __func__, __LINE__, g_ConfigData.uart3.data_bit);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_PARITY);
    if (tmp != NULL)
    {
        g_ConfigData.uart3.parity = *tmp;
    }
    printf("[%s][%d] g_ConfigData.uart3.parity is %c\n", __func__, __LINE__, g_ConfigData.uart3.parity);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_STOP_BIT);
    if (tmp != NULL)
    {
       g_ConfigData.uart3.stop_bit = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart3.stop_bit is %d\n", __func__, __LINE__, g_ConfigData.uart3.stop_bit);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.uart3.slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart3.slave_id is %d\n", __func__, __LINE__, g_ConfigData.uart3.slave_id);   

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, REQUEST_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.uart3.request_slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.uart3.request_slave_id is %d\n", __func__, __LINE__, g_ConfigData.uart3.request_slave_id);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_MODE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.uart3.mode, tmp);
    }
    printf("[%s][%d] g_ConfigData.uart3.mode is %s\n", __func__, __LINE__, g_ConfigData.uart3.mode);

    /*fan1 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_FAN1);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci fan1 section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.fan1.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.fan1.enable is %s\n", __func__, __LINE__, g_ConfigData.fan1.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.fan1.slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.fan1.slave_id is %d\n", __func__, __LINE__, g_ConfigData.fan1.slave_id);

    /*fan2 config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_FAN2);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci fan2 section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.fan2.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.fan2.enable is %s\n", __func__, __LINE__, g_ConfigData.fan2.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.fan2.slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.fan2.slave_id is %d\n", __func__, __LINE__, g_ConfigData.fan2.slave_id);

    /*iso config*/
    uci_sect = uci_lookup_section(uci_ctx, uci_pack, SECTION_ISO);
    if (uci_sect == NULL)
    {
        printf("[%s][%d]lookup uci iso section fail!\n", __func__, __LINE__);
        uci_unload(uci_ctx, uci_pack);
        uci_free_context(uci_ctx);
        return -1;
    }

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, ENABLE);
    if (tmp != NULL)
    {
        strcpy(g_ConfigData.iso.enable, tmp);
    }
    printf("[%s][%d] g_ConfigData.iso.enable is %s\n", __func__, __LINE__, g_ConfigData.iso.enable);

    tmp = NULL;
    tmp = uci_lookup_option_string(uci_ctx, uci_sect, UART_SLAVE_ID);
    if (tmp != NULL)
    {
        g_ConfigData.iso.slave_id = atoi(tmp);
    }
    printf("[%s][%d] g_ConfigData.iso.slave_id is %d\n", __func__, __LINE__, g_ConfigData.iso.slave_id);

    uci_unload(uci_ctx, uci_pack);
    uci_free_context(uci_ctx);
    return -1;

}

const ConfigData* get_config_data_instance() {
    return &g_ConfigData;
}
